package zhuyun.code.launcheractivity.view

import android.app.Activity
import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.KeyEvent
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import android.widget.TextView
import io.objectbox.Box
import kotlinx.android.synthetic.main.activity_product_list_search.*
import zhuyun.code.launcheractivity.App
import zhuyun.code.launcheractivity.R
import zhuyun.code.launcheractivity.model.Constants
import zhuyun.code.launcheractivity.model.db.DBB2CSearchHistory
import zhuyun.code.launcheractivity.model.db.DBB2CSearchHistory_
import zhuyun.code.launcheractivity.view.adapter.B2CSearchHistoryAdapter
import zhuyun.code.launcheractivity.view.widget.component.FlowLayoutManager

class ProductListSearchActivity : BaseActivity() {
    private lateinit var dbb2CSearchHistoryBox: Box<DBB2CSearchHistory>
    private val keyWordsList = mutableListOf<DBB2CSearchHistory>()
    private lateinit var b2CSearchHistoryAdapter:B2CSearchHistoryAdapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_product_list_search)
        dbb2CSearchHistoryBox = App.context.boxStore.boxFor(DBB2CSearchHistory::class.java)
        initViews()
    }

    private fun deleteAllSearchHistoryFromDatabase(){
        dbb2CSearchHistoryBox.removeAll()
    }


    private fun refreshHistoryList(){
        keyWordsList.clear()
        keyWordsList.addAll(dbb2CSearchHistoryBox.query().build().find())
        b2CSearchHistoryAdapter.notifyDataSetChanged()
    }



    private fun initViews(){
        keyWordsList.addAll(dbb2CSearchHistoryBox.query().build().find())
        b2CSearchHistoryAdapter = B2CSearchHistoryAdapter(keyWordsList)
        b2CSearchHistoryAdapter.mSearchHistoryItemClickListener = object :B2CSearchHistoryAdapter.SearchHistoryItemClickListener{
            override fun onItemClick(item: DBB2CSearchHistory) {
                val intent = Intent()
                intent.putExtra(Constants.KeyWord,item.keyWord)
                setResult(Activity.RESULT_OK,intent)
                finish()
            }

        }
        recyclerView.layoutManager = FlowLayoutManager()
        recyclerView.adapter = b2CSearchHistoryAdapter
        ivClear.setOnClickListener{
            deleteAllSearchHistoryFromDatabase()
            refreshHistoryList()
        }
        etSearch.setOnEditorActionListener { _, actionId, _ ->
            if(actionId == EditorInfo.IME_ACTION_SEARCH){
                (getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager)
                    .hideSoftInputFromWindow(currentFocus!!.windowToken,InputMethodManager.HIDE_NOT_ALWAYS)
                val keyWord = etSearch.text.toString().trim()
                if(keyWord.isNotEmpty()){
                    insertToDb(keyWord)
                }
                val intent = Intent()
                intent.putExtra(Constants.KeyWord,keyWord)
                setResult(Activity.RESULT_OK,intent)
                finish()
            }
            false
        }
    }

    private fun insertToDb(keyWord:String){
        val dbb2CSearchHistories = dbb2CSearchHistoryBox.query().equal(DBB2CSearchHistory_.keyWord,keyWord).build().find()
        if(dbb2CSearchHistories.isEmpty()){
            val dbB2CSearchHistory = DBB2CSearchHistory(keyWord=keyWord,isSelected = false)
            dbb2CSearchHistoryBox.put(dbB2CSearchHistory)
        }
    }

}
